package com.ruicar.afs.cloud.workflow.gwt.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.admin.api.dto.UserSimpleInfoDto;
import com.ruicar.afs.cloud.admin.api.feign.AfsDeptFeign;
import com.ruicar.afs.cloud.admin.api.feign.AfsRoleFeign;
import com.ruicar.afs.cloud.admin.api.feign.AfsUserFeign;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.vo.RoleVo;
import com.ruicar.afs.cloud.common.core.security.vo.UserVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.enums.GreatWallFlowParamType;
import com.ruicar.afs.cloud.workflow.api.dto.FlowParam;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.CommonGetRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.GetAssignUserRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.UserOnlineRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
@AllArgsConstructor
public class DefaultAfsWorkFLowQueryProcessor implements AfsWorkFLowQueryProcessor {
    private final AfsDeptFeign afsDeptFeign;
    private final AfsUserFeign afsUserFeign;
    private final AfsRoleFeign afsRoleFeign;

    @Override
    public List<String> afsFLowKeys() {
        return Arrays.asList(new String[]{"default"});
    }

    @Override
    public List<UserOnlineResponse> checkUserOnline(UserOnlineRequest userOnlineRequest) {
        try {
            log.info("检查用户是否在线", JSON.toJSONString(userOnlineRequest));
            IResponse<List<UserSimpleInfoDto>> listIResponse = afsUserFeign.getSimpleInfoByUserLoginNames(userOnlineRequest.getUserInfos().stream().map(UserOnlineRequest.UserInfo::getUserName).collect(Collectors.toList()));
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(listIResponse.getCode()), "feign请求用户异常");
            return listIResponse
                    .getData()
                    .stream()
                    .map(userSimpleInfoDto -> {
                        UserOnlineResponse userOnlineResponse = new UserOnlineResponse();
                        userOnlineResponse.setUserName(userSimpleInfoDto.getUserName());
                        userOnlineResponse.setOnLine(true);
                        return userOnlineResponse;
                    }).collect(Collectors.toList());
        } catch (Exception e) {
            log.error("", e);
        }
        return new ArrayList<>();
    }

    @Override
    public GetAssignUserResponse getAssignUser(GetAssignUserRequest getAssignUserRequest) {
        try {
            IResponse<List<UserSimpleInfoDto>> listIResponse = afsUserFeign.getSimpleUserListByRoleCode(getAssignUserRequest.getRoleCode());
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(listIResponse.getCode()), "feign请求用户异常");
            return listIResponse
                    .getData()
                    .stream()
                    .map(userSimpleInfoDto -> {
                        GetAssignUserResponse getAssignUserResponse = new GetAssignUserResponse();
                        getAssignUserResponse.setUserName(userSimpleInfoDto.getUserName());
                        return getAssignUserResponse;
                    }).findAny().get();
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    @Override
    public QueryOrgConditionResponse queryOrgCondition(CommonGetRequest commonGetRequest) {
        QueryOrgConditionResponse queryOrgConditionResponse = new QueryOrgConditionResponse();
        queryOrgConditionResponse.setParamList(new ArrayList<>());
        FlowParam flowParam = new FlowParam("部门名称", "orgName");
        flowParam.setType(GreatWallFlowParamType.INPUT);
        queryOrgConditionResponse.getParamList().add(flowParam);

        FlowParam userNameParam = new FlowParam("用户名", "userName");
        userNameParam.setType(GreatWallFlowParamType.INPUT);
        queryOrgConditionResponse.getParamList().add(userNameParam);
        return queryOrgConditionResponse;
    }

    @Override
    public Page<QueryUserResponse> queryUserInfo(QueryCondition<Map<String, Object>> queryCondition) {
        UserSimpleInfoDto userSimpleInfoDto = new UserSimpleInfoDto();
        if (queryCondition.getCondition() != null && queryCondition.getCondition().containsKey("orgName")) {
            userSimpleInfoDto.setDeptName(queryCondition.getCondition().get("orgName").toString());
        }
        if (queryCondition.getCondition() != null && queryCondition.getCondition().containsKey("userName")) {
            userSimpleInfoDto.setUserRealName(queryCondition.getCondition().get("userName").toString());
        }
        QueryCondition<UserSimpleInfoDto> simpleInfoDtoQueryCondition = new QueryCondition<>();
        simpleInfoDtoQueryCondition.setPageSize(queryCondition.getPageSize());
        simpleInfoDtoQueryCondition.setPageNumber(queryCondition.getPageNumber());
        simpleInfoDtoQueryCondition.setCondition(userSimpleInfoDto);
        try {
            IResponse<Page<UserSimpleInfoDto>> pageQuerySimpleUserInfo = afsUserFeign.pageQuerySimpleUserInfo(simpleInfoDtoQueryCondition);
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(pageQuerySimpleUserInfo.getCode()), "feign请求用户异常");
            Page<QueryUserResponse> page = new Page<>();
            page.setCurrent(pageQuerySimpleUserInfo.getData().getCurrent());
            page.setTotal(pageQuerySimpleUserInfo.getData().getTotal());
            page.setSize(pageQuerySimpleUserInfo.getData().getSize());
            page.setRecords(
                    pageQuerySimpleUserInfo
                            .getData()
                            .getRecords()
                            .stream()
                            .map(userSimpleInfoDto1 -> {
                                QueryUserResponse queryUserResponse = new QueryUserResponse();
                                queryUserResponse.setUserName(userSimpleInfoDto1.getUserName());
                                queryUserResponse.setEmail(userSimpleInfoDto1.getUserEmail());
                                queryUserResponse.setOrgId(userSimpleInfoDto1.getDeptId());
                                queryUserResponse.setOrgName(userSimpleInfoDto1.getDeptName());
                                queryUserResponse.setPhoneNumber(userSimpleInfoDto1.getPhone());
                                queryUserResponse.setUserRealName(userSimpleInfoDto1.getUserRealName());
                                return queryUserResponse;
                            }).collect(Collectors.toList())
            );
            return page;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }


    @Override
    public QueryRoleResponse queryRole(CommonGetRequest commonGetRequest) {
        try {
            IResponse<List<RoleVo>> iResponse = afsRoleFeign.getAllList();
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(iResponse.getCode()), "feign请求用户异常");
            QueryRoleResponse queryRoleResponse = new QueryRoleResponse();
            queryRoleResponse.setRoleVos(iResponse.getData());
            return queryRoleResponse;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    @Override
    public UserVo getRtnUserInfo(CommonGetRequest commonGetRequest) {
        UserVo userVo = new UserVo();
        String[] userNames = commonGetRequest.getBodyData().split(",");
        if (userNames.length == 1) {
            userVo.setUsername(commonGetRequest.getBodyData());
        } else {
            int random = (int) (Math.random() * 1000) % userNames.length;
            userVo.setUsername(userNames[random]);
        }
        return userVo;
    }

    @Override
    public UserVo getApplyUserInfo(CommonGetRequest commonGetRequest) {
        //默认不实现
        return new UserVo();
    }

    @Override
    public UserVo getLeader(CommonGetRequest commonGetRequest) {
        //默认不实现
        return new UserVo();
    }

    @Override
    public UserVo getOperUser(CommonGetRequest commonGetRequest) {
        //默认不实现
        return new UserVo();
    }
}
